using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.CRMDomain;
//using ShivamEMR.Web.Areas.CRM.Models;
using ShivamEMR.Model.DataInterfaces.CompanyDataInterf;
using ShivamEMR.Model.DataInterfaces.CRMDataInterf;
using ShivamEMR.Model;

namespace ShivamEMR.Web.Areas.CRM.Controllers
{   
    public class AppointmentController : Controller
    {
		private readonly IEmployeeRepository employeeRepository;
		private readonly IAppointmentGroupRepository appointmentgroupRepository;
		private readonly IVenueRepository venueRepository;
		private readonly IAppointmentStatusRepository appointmentstatusRepository;
		private readonly IAppointmentRepository appointmentRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public AppointmentController()// : this(new EmployeeRepository(), new AppointmentGroupRepository(), new VenueRepository(), new AppointmentStatusRepository(), new AppointmentRepository())
        {
            this.employeeRepository = Registry.RepositoryFactory.GetEmployeeRepository();
            this.appointmentgroupRepository = Registry.RepositoryFactory.GetAppointmentGroupRepository();
            this.appointmentstatusRepository = Registry.RepositoryFactory.GetAppointmentStatusRepository();
            this.venueRepository = Registry.RepositoryFactory.GetVenueRepository();
            this.appointmentRepository = Registry.RepositoryFactory.GetAppointmentRepository();
        }

        public AppointmentController(IEmployeeRepository employeeRepository, IAppointmentGroupRepository appointmentgroupRepository, IVenueRepository venueRepository, IAppointmentStatusRepository appointmentstatusRepository, IAppointmentRepository appointmentRepository)
        {
			this.employeeRepository = employeeRepository;
			this.appointmentgroupRepository = appointmentgroupRepository;
			this.venueRepository = venueRepository;
			this.appointmentstatusRepository = appointmentstatusRepository;
			this.appointmentRepository = appointmentRepository;
        }

        //
        // GET: /Appointment/

        public ViewResult Index()
        {
            return View(appointmentRepository.All);//Including(appointment => appointment.Employee, appointment => appointment.Venue, appointment => appointment.AppointmentWith));
        }

        //
        // GET: /Appointment/Details/5

        public ViewResult Details(int id)
        {
            return View(appointmentRepository.Find(id));
        }

        //
        // GET: /Appointment/Create

        public ActionResult New(Guid id)
        {
            ViewBag.PossibleEmployees = employeeRepository.All;
            ViewBag.PossibleAppointmentGroups = appointmentgroupRepository.All;
            ViewBag.PossibleVenues = venueRepository.All;
            ViewBag.PossibleAppointmentStatus = appointmentstatusRepository.All;
            var app = new Appointment() { DateAndTime=DateTime.Now, Priority=1 };
            return View(app);
        }

          [HttpPost]
        public ActionResult New(Guid id, Appointment appointment)
        {
            if  (appointment!=null)// (ModelState.IsValid)
            {
                appointment.AppointmentGroupID = 1;
                appointment.AppointmentStatusID = 1;
                appointment.AppointmentWith = new List<ShivamEMR.Model.Domain.PersonDomain.Person>();
                appointment.AppointmentWith.Add(appointmentRepository.GetPerson(id));
                appointmentRepository.Create(appointment);
                appointmentRepository.Save();
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.PossibleEmployees = employeeRepository.All;
                ViewBag.PossibleAppointmentGroups = appointmentgroupRepository.All;
                ViewBag.PossibleVenues = venueRepository.All;
                ViewBag.PossibleAppointmentStatus = appointmentstatusRepository.All;
                return View();
            }
        } 

        public ActionResult Create()
        {
			ViewBag.PossibleEmployees = employeeRepository.All;
			ViewBag.PossibleAppointmentGroups = appointmentgroupRepository.All;
			ViewBag.PossibleVenues = venueRepository.All;
			ViewBag.PossibleAppointmentStatus = appointmentstatusRepository.All;
            return View();
        } 

        //
        // POST: /Appointment/Create

        [HttpPost]
        public ActionResult Create(Appointment appointment)
        {
            if (ModelState.IsValid) {
                appointmentRepository.Create(appointment);
                appointmentRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleEmployees = employeeRepository.All;
				ViewBag.PossibleAppointmentGroups = appointmentgroupRepository.All;
				ViewBag.PossibleVenues = venueRepository.All;
				ViewBag.PossibleAppointmentStatus = appointmentstatusRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Appointment/Edit/5
 
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleEmployees = employeeRepository.All;
			ViewBag.PossibleAppointmentGroups = appointmentgroupRepository.All;
			ViewBag.PossibleVenues = venueRepository.All;
			ViewBag.PossibleAppointmentStatus = appointmentstatusRepository.All;
             return View(appointmentRepository.Find(id));
        }

        //
        // POST: /Appointment/Edit/5

        [HttpPost]
        public ActionResult Edit(Appointment appointment)
        {
            if (ModelState.IsValid) {
                appointmentRepository.Update(appointment);
                appointmentRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleEmployees = employeeRepository.All;
				ViewBag.PossibleAppointmentGroups = appointmentgroupRepository.All;
				ViewBag.PossibleVenues = venueRepository.All;
				ViewBag.PossibleAppointmentStatus = appointmentstatusRepository.All;
				return View();
			}
        }

        //
        // GET: /Appointment/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(appointmentRepository.Find(id));
        }

        //
        // POST: /Appointment/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var a = appointmentRepository.Find(id);
            appointmentRepository.Delete(a);
            appointmentRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

